/**
* 作者: clz
* 日期: 2025-07-25 16:19:59
* 备注: 联盟动态记录
*/
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.CsGameUnion;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.MailManager;
import com.motu.monstercity.server.game.logic.UnionManager;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.redis.RedisTools;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;

public class UnionDynamicRecord extends UnionDynamicRecordModel {
	public UnionDynamicRecord() {
		super();
	}

	public UnionDynamicRecord( long unionId, String msg, String msgReplace) {
		putUnionId(unionId);
		putMsg(msg);
		putMsgReplace(msgReplace);
		putAddTime(TimeUtils.getCurTime());
	}

	public void doCreate() {
		setTableId();
		insert();
	}

	public JsonObject insert() {
		// 如果超过上限则会删除最早的那条
		long count = Future.await(RedisTools.getHashLen(getListKey()));
		if (count >= AllParam.UNION_DYNAMIC_LIST_MAX_NUM) {
			String orderListKey = EntityManager.getOrderListKey(getListKey());
			String delRecord = Future.await(RedisTools.lpop(orderListKey));// 删除列表数据
			long id = Long.parseLong(delRecord);// dbid
			UnionDynamicRecord unionDynamicRecord = UnionManager.getUnionDynamicRecord(getUnionId(),id);
			if (unionDynamicRecord != null) {
				unionDynamicRecord.delete();
			}
		}
		return super.insert();
	}

	public CsGameUnion.PBUnionDynamicRecord.Builder takeBuilder(int language){
		CsGameUnion.PBUnionDynamicRecord.Builder pb = CsGameUnion.PBUnionDynamicRecord.newBuilder();
		pb.setAddTime(getAddTime());
		pb.setMsg(MailManager.takeWordSting(getMsg(), language, getMsgReplace()));
		return pb;
	}
}
